package IO.g2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Scanner;

public class Student implements Serializable{

	private static final long serialVersionUID = -5156536141019279293L;
	private String name;
	private int studyID;
	private String department;
	private transient Thread t = new Thread();
	
	public Student(String name, int studyID, String department) {
		this.name = name;
		this.studyID = studyID;
		this.department = department;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getStudyID() {
		return studyID;
	}

	public void setStudyID(int studyID) {
		this.studyID = studyID;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}
	
	public boolean writeData(BufferedWriter out) {
		try {
			out.write(name + ":" + studyID + ":" + department);
			out.append(System.getProperty("line.separator"));
		} catch (IOException e) {
			return false;
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				return false;
			}
		}
		return true;
	}

	public static void writeData(BufferedWriter out, Student[] s) {
		try {
			for (Student student : s) {
				String name = student.getName();
				int studyID = student.getStudyID();
				String department = student.getDepartment();

				out.write(name + ":" + studyID + ":" + department);
				out.append(System.getProperty("line.separator"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Student readData(BufferedReader in){
		try {
			String readline = in.readLine();

			String[] split = readline.split(":");
			String name = split[0];
			int studyID = Integer.parseInt(split[1]);
			String department = split[2];
			return new Student(name, studyID, department);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static Student[] readData(BufferedReader in, int count){
		Student[] students = new Student[count];
		try {
			LineNumberReader reader = new LineNumberReader(in);
			String readline = "";
			while ((readline = reader.readLine()) != null && reader.getLineNumber() <= count) {
				String[] split = readline.split(":");
				String name = split[0];
				int studyID = Integer.parseInt(split[1]);
				String department = split[2];
				students[reader.getLineNumber() - 1] = new Student(name,
						studyID, department);
			}
			return students;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public boolean writeData(PrintWriter out) {
		out.print(name);
		out.print(':');
		out.print(studyID);
		out.print(":");
		out.println(department);
//		out.append(System.getProperty("line.separator"));
		if (out.checkError()) {
			out.close();
			return false;
		}
		out.close();
		return true;
	}

	public Student readData(Scanner in) {
		String oneStudent = in.nextLine();
		String[] split = oneStudent.split(":");
		return new Student(split[0], Integer.parseInt(split[1]), split[2]);
	}
	
	public void writeData(DataOutputStream out){
		try {
			out.writeChars(name);
			out.writeChar(':');
			out.writeInt(studyID);
			out.writeChar(':');
			out.writeChars(department);
			out.writeChars(System.getProperty("line.separator"));
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Student readData(DataInputStream in){
		try {
			String name = "";
			char chr;
			while((chr = in.readChar())!= ':'){
				name += chr;
			}
			int studyID = in.readInt();
			in.readChar();
			String department = "";
			char sep = System.getProperty("line.separator").charAt(0);
			while((chr = in.readChar())!= sep){
				department += chr;
			}
			return new Student(name, studyID, department);
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	
	
	
	
	
}
